package view 
{
	import com.adobe.air.filesystem.events.FileMonitorEvent;
	import com.greensock.TweenMax;
	import controller.Const;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.utils.Timer;
	import model.GameData;
	import model.MyData;
	import sound.SoundManager;
	import view.result.ResultAnimal;
	
	/**
	 * ...
	 * @author @Boy_Kute
	 */
	public class ShowResult extends Base 
	{
		private var _bgResult:MovieClip;
		
		private var _boatMenDanhca:MovieClip;
		private var _timeShowResult:Timer;
		private var _treasure:MovieClip;
		private var _titleBKQ:MovieClip;
		private var _componentResult:MovieClip;
		private var _sumResultImg:MovieClip;
		
		private var _arrTreasure:Array = [];
		private var _boatmenQuangLuoi:MovieClip;
		
		private var _arrComponentResult:Array = [];
		private var _arrShowBetting:Array = [];
		
		
		
		private var _count:int = 0;
		private var _countShow:int = 0;
		private var _sumResult:int = 0;
		
		private var _timerShowResult:Timer = new Timer(18000);
		private var _timerQuangLuoi:Timer;
		
		private var _arrImg:Array = [];
		private var _arrImgAdd:Array = [];
		private var _ca:MovieClip;
		private var _cua:MovieClip;
		private var _sua:MovieClip;
		private var _oc:MovieClip;
		private var _tom:MovieClip;
		private var _rua:MovieClip;
		private var _cangua:MovieClip;
		private var _ranbien:MovieClip;
		private var _sam:MovieClip;
		private var _caugai:MovieClip;
		
		public function ShowResult() 
		{
			if (GameData.getInstance().checkSoundOn) 
			{
				SoundManager.getInstance().playSound("Keoca", 1000);
			}
			
			
			_boatmenQuangLuoi = getImage("BoatmenQuangLuoi", "playGame");
			addChild(_boatmenQuangLuoi);
			_boatmenQuangLuoi.scaleX = _boatmenQuangLuoi.scaleY = 0.5;
			_boatmenQuangLuoi.x = 550;
			_boatmenQuangLuoi.y = 200;
			
			
			
			_timerQuangLuoi = new Timer(6000, 1)
			_timerQuangLuoi.addEventListener(TimerEvent.TIMER_COMPLETE, quangLuoi);
			_timerQuangLuoi.start();
			
			
		}
		
		private function quangLuoi(e:TimerEvent):void 
		{
			_timerQuangLuoi.removeEventListener(TimerEvent.TIMER_COMPLETE, quangLuoi);
			_timerQuangLuoi.stop();
			_boatmenQuangLuoi.visible = false;
			
			dispatchEvent(new Event("completeQuangLuoi"))
		}
		
		
		public function showResult(arrResult:Array):void 
		{
			_boatMenDanhca = getImage("BroatmenKeoCa", "playGame");
			addChild(_boatMenDanhca);
			_boatMenDanhca.scaleX = _boatMenDanhca.scaleY = 0.5
			_boatMenDanhca.x = 600;
			_boatMenDanhca.y = 230;
			
			_ca = getImage("ca", "playGame");
			_cua = getImage("cua", "playGame");
			_sua = getImage("sua", "playGame");
			_oc = getImage("oc", "playGame");
			_tom = getImage("tom", "playGame");
			_rua = getImage("rua", "playGame");
			_cangua = getImage("cangua", "playGame");
			_ranbien = getImage("ranbien", "playGame");
			_sam = getImage("caduoi", "playGame");
			_caugai = getImage("caugai", "playGame");
			
			_arrImg = [_ca, _cua, _sua, _oc, _tom, _rua, _cangua, _ranbien, _sam, _caugai];
			
			_bgResult = getImage("titleBKQ", "playGame");
			addChild(_bgResult);
			_bgResult.x = 1150;
			_bgResult.y = 150;
			_sumResultImg = getImage("sumResult", "playGame");
			addChild(_sumResultImg);
			var i:int
			var j:int
			var xLoc:int;
			var yLoc:int;
			_sumResult = 0;
			_countShow = 0;
			for (i = 0; i < arrResult.length; i++) 
			{
				_sumResult = _sumResult + int(arrResult[i]);
				_sumResultImg.txtSumResult.text = "Tổng: " + String(_sumResult);
				if (int(arrResult[i]) > 0)
				{
					_countShow++;
					_componentResult = getImage("componentBKQ", "playGame");
					addChild(_componentResult);
					_componentResult.x = 1150;
					_componentResult.y = 240 + (_countShow - 1) * _componentResult.height;
					_arrComponentResult.push(_componentResult);
					_componentResult.gotoAndStop(i + 1);
					_componentResult.txtResult.text = String(arrResult[i])
					
					addChild(_arrImg[i]);
					_arrImgAdd.push(_arrImg[i]);
					_arrImg[i].scaleX = _arrImg[i].scaleY = 0.4;
					_arrImg[i].x = 820 + 25 * xLoc;
					_arrImg[i].y = 320 + 40 * yLoc;
					if (xLoc < 4)
					{
						xLoc ++;
					}
					else
					{
						xLoc = 0;
						yLoc ++;
					}
					
					TweenMax.to(_arrImg[i], 14, {bezier:[{x:775, y:260}], alpha:0});
				}
				
				if (int(arrResult[i]) > 1)
				{
					_treasure = getImage("Treasure", "playGame")
					addChild(_treasure);
					_treasure.scaleX = _treasure.scaleY = 0.7;
					_treasure.homphanthuong.gotoAndStop(3);
					_treasure.x = 1153;
					_treasure.y = 600;
					_treasure.gotoAndStop(i+1);
					_treasure.txtKhoBau.text = "x " + String(arrResult[i]);
					_arrTreasure.push(_treasure);
				}
			}
			
			//trace("_arrComponentResult", _arrComponentResult.length, _arrComponentResult[_arrComponentResult.length - 1].x , _arrComponentResult[_arrComponentResult.length - 1].height)
			
			_sumResultImg.x = 1150;
			//_sumResultImg.y = _arrComponentResult[_arrComponentResult.length - 1].x + _arrComponentResult[_arrComponentResult.length - 1].height;
			_sumResultImg.y = 210;
			
			_timerShowResult.addEventListener(TimerEvent.TIMER, startResult);
			_timerShowResult.start();
		}
		
		
		private function startResult(e:TimerEvent):void 
		{
			_timerShowResult.stop();
			_timerShowResult.removeEventListener(TimerEvent.TIMER, startResult);
			
			dispatchEvent(new Event("showMoneyUser"));
		}
		
		private function completeTween():void 
		{
			//_checkComplete = true;
		}
		
		public function removeAllEvent():void
		{
			//_bgResult.txtSumCL.text = "0";
			SoundManager.getInstance().stopSound("Keoca");
			//_timeShowResult.stop();
			//_timeShowResult.removeEventListener(TimerEvent.TIMER, keoLuoi);
			
			_timerShowResult.stop();
			_timerShowResult.removeEventListener(TimerEvent.TIMER, startResult);
			
			var i:int;
			if (_arrImg && _arrImg.length > 0)
			{
				for (i = 0; i < _arrImg.length; i++) 
				{
					//removeChild(_arrImg[i]);
					_arrImg[i] = null;
				}
			}
			
			
			if (_arrImgAdd && _arrImgAdd.length > 0)
			{
				for (i = 0; i < _arrImgAdd.length; i++) 
				{
					removeChild(_arrImgAdd[i]);
					_arrImgAdd[i] = null;
				}
			}
			if (_boatMenDanhca)
			{
				removeChild(_boatMenDanhca);
				_boatMenDanhca = null;
			}

			if (_arrComponentResult && _arrComponentResult.length > 0)
			{
				for (i = 0; i < _arrComponentResult.length; i++) 
				{
					removeChild(_arrComponentResult[i]);
					_arrComponentResult[i] = null;
				}
			}
			if (_arrTreasure && _arrTreasure.length > 0)
			{
				for (i = 0; i < _arrTreasure.length; i++) 
				{
					removeChild(_arrTreasure[i]);
					_arrTreasure[i] = null;
				}
			}
			
			if (_boatMenDanhca)
			{
				removeChild(_boatMenDanhca);
				_boatMenDanhca = null;
			}
			
			if (_bgResult)
			{
				removeChild(_bgResult);
				_bgResult = null;
			}
			
			if (_sumResultImg)
			{
				removeChild(_sumResultImg);
				_sumResultImg = null;
			}
			
		
			TweenMax.killAll();
			_arrImgAdd = [];
			_arrTreasure = [];
			_arrComponentResult = [];
			_arrImg = [];
			_count = 0;
			_countShow = 0;

			
		}
	}

}